import * as shop from '@/api/shop'
const state = {
  name: 'product',
  all: []
}
const mutations = {
  setProducts (state, playload) {
    state.all = playload.products
  },
  decrementProductInventory (state, playload) {
    const product = state.all.find(el => el.id === playload.id)
    if (product.inventory > 0) {
      product.inventory--
    }
  }
}

const getters = {}

const actions = {
  async getAllProducts ({ commit }) {
    const products = await shop.getAllProducts()
    commit({
      type: 'setProducts',
      products
    })
  }
}

export default {
  namespaced: true, // 开启命名空间
  state,
  mutations,
  getters,
  actions
}
